package com.requiem.hist_group_manage_springboot.common;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Map;

@Component
public class JWTUtil {
    @Value("${privatization.jwt-secret}")
    private String JWT_SECRET;

    public String createToken(Map<String, Object> claims) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR_OF_DAY, 24);
        JWTCreator.Builder builder = JWT.create();

        return builder
                .withExpiresAt(calendar.getTime())
                .withClaim("user_id", (int) claims.get("user_id"))
                .sign(Algorithm.HMAC256(JWT_SECRET));
    }

    public DecodedJWT verifyToken(String token) {
        return JWT.require(Algorithm.HMAC256(JWT_SECRET)).build().verify(token);
    }
}
